package ynwx.libaray.base;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import ynwx.libaray.utils.ActivityCollector;
import ynwx.libaray.utils.LogUtil;
import ynwx.libaray.utils.SharedPreferenceUtil;
import ynwx.mylibrary.R;

/**
 * Created by Chron on 2017/11/13.
 */

public abstract class BaseActivity extends AppCompatActivity {
    public static final boolean D = true;

    protected Toolbar toolbar;
    protected TextView toolbarTitle;
    protected String userToken;
    protected String TAG = "BaseActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userToken = (String) SharedPreferenceUtil.get(getApplicationContext(), "userToken", "noToken");
        ActivityCollector.addActivity(this, getClass());
        ActivityCollector.getActivityCollector().addActivity(this);
        initData();
        setContentView();
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setResume();
        setOnclick();
    }

    public abstract void initData();

    public abstract void setContentView();

    public abstract void initView();

    public abstract void setResume();

    public abstract void setOnclick();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
        ActivityCollector.getActivityCollector().finishActivity(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        if (displayToolbarEnabled()) {
            initToolbar();
        }
    }

    /**
     * 结束页面的时候将页面中相关控件移除
     * http://blog.csdn.net/newlai913/article/details/78095873
     */
    @Override
    public void finish() {
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        view.removeAllViews();
        super.finish();
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        if (D) LogUtil.i(this, "onTitleChanged title: " + title);
        if (toolbarTitle != null) {
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            toolbarTitle.setText(title);
            if (D) LogUtil.i(this, "onTitleChanged tvToolbarTitle: " + toolbarTitle.getText());
        }
    }

    private void initToolbar() {
        View v = findViewById(R.id.toolbar);
        if (v != null) {
            toolbar = (Toolbar) v;
            setSupportActionBar(toolbar);
            toolbarTitle = v.findViewById(R.id.toolbar_title);
            if (toolbarTitle != null) {
                getSupportActionBar().setDisplayShowTitleEnabled(false);
            }
            if (displayHomeAsUpEnabled()) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }
        }
    }

    protected boolean displayHomeAsUpEnabled() {
        return true;
    }

    protected boolean displayToolbarEnabled() {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return true;
    }

    /**
     * 去重吐司
     *
     * @param context 上下文
     * @param text 显示信息
     */
    static Toast mToast;

    public static void showToast(Context context, String text) {
        if (mToast == null) {
            mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(text);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    /**
     * 倒计时设计
     */
    public class VirifyCountDownTimer extends CountDownTimer {
        private TextView tvCountDown;

        public VirifyCountDownTimer(TextView textView, long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            tvCountDown = textView;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tvCountDown.setClickable(false);
            String reacquire = String.format(getString(R.string.login_after_time_reacquire_virification),
                    millisUntilFinished / 1000);
            tvCountDown.setText(reacquire);  //设置倒计时时间
        }

        @Override
        public void onFinish() {
            tvCountDown.setClickable(true);
            tvCountDown.setText("");
        }
    }
}
